package cn.hongwei.service.base.handler;

import cn.hongwei.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//全局异常处理
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R error(Exception e){
        //{} 代表占位符
        log.debug("{}级别日志" , "debug");
        log.info("{}级别日志" , "info");
        log.warn("{}级别日志" , "warn");
        log.error("{}级别日志" ,e);
        return R.error();
    }

    //捕获具体的异常
    @ExceptionHandler(ArithmeticException.class)
    public R arithmeticException(ArithmeticException e){
        //{} 代表占位符
        log.debug("{}级别日志" , "debug");
        log.info("{}级别日志" , "info");
        log.warn("{}级别日志" , "warn");
        log.error("{}级别日志" ,e.getMessage());
        e.printStackTrace();
        return R.error().message("0不能作为除数");
    }
}